package com.zy.xuanwo.acitivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;
import com.zhy.http.okhttp.request.CountingRequestBody;
import com.zy.xuanwo.R;
import com.zy.xuanwo.application.MyApplication;
import com.zy.xuanwo.utils.RegexUtils;
import com.zy.xuanwo.utils.ToastUtils;
import com.zy.xuanwo.utils.URLConstants;
import com.zy.xuanwo.utils.UserInfoSPManager;
import com.zy.xuanwo.view.CleanEditText;
import com.zy.xuanwo.view.CustomToast;
import com.zy.xuanwo.zyc.portal.bean.ErrorMessag;
import com.zy.xuanwo.zyc.portal.callback.GetCommonResultListener;
import com.zy.xuanwo.zyc.portal.service.imp.PortalMagger;

import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.RequestBody;

/**
 * @功能 修改密码
 * Created by njb on 2016/7/8 0008.
 */
public class UpdatePasswordActivity extends AppCompatActivity implements View.OnClickListener {


    @Bind(R.id.back_iv)
    ImageView backIv;
    @Bind(R.id.title_update_password)
    TextView titleUpdatePassword;
    @Bind(R.id.save_tv)
    TextView saveTv;
    @Bind(R.id.title_layout_rl)
    RelativeLayout titleLayoutRl;
    @Bind(R.id.current_password_tv)
    TextView currentPasswordTv;
    @Bind(R.id.edit_current_password)
    CleanEditText editCurrentPassword;
    @Bind(R.id.current_password_rl)
    RelativeLayout currentPasswordRl;
    @Bind(R.id.current_password_view)
    View currentPasswordView;
    @Bind(R.id.new_password_tv)
    TextView newPasswordTv;
    @Bind(R.id.edit_new_password)
    CleanEditText editNewPassword;
    @Bind(R.id.new_password_rl)
    RelativeLayout newPasswordRl;
    @Bind(R.id.new_password_view)
    View newPasswordView;
    @Bind(R.id.sure_password_tv)
    TextView surePasswordTv;
    @Bind(R.id.edit_sure_new_password)
    CleanEditText editSureNewPassword;

    public static final int UPDATE_PASSWORD_SUCCESS = 0x22;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_password);
        ButterKnife.bind(this);
    }

    private void updatePassword() {
        String password = editCurrentPassword.getText().toString().trim();
        String newPassword = editNewPassword.getText().toString().trim();
        String sureNewPassword = editSureNewPassword.getText().toString().trim();

        if (checkInput(password, newPassword, sureNewPassword)) {

            RequestBody formBody = new FormBody.Builder()
                    .add("username", UserInfoSPManager.getUserName())
                    .add("password", password)
                    .add("new_password", newPassword)
                    .build();

            OkHttpUtils.put()
                    .url(URLConstants.UPDATE_PASSWORD_URL)
                    .addHeader("token", "446f8fd7-8fc3-426d-8c70-283c021dd380")
                    .addHeader("X-Request-Sign", "f033abaf9eac94ffc03d25ee6be445e3, 1467598209")
                    .addHeader("X-Application-Id", "3d632345-abd5-4c7c-b157-6d12cd3f0dde")
                    .requestBody(formBody)
                    .build()
                    .execute(new StringCallback() {
                        @Override
                        public void onError(Call call, Exception e, int id) {
                            Log.e("caca", e + "");
                        }

                        @Override
                        public void onResponse(String response, int id) {
                            Log.e("caca", response + "");
                            ToastUtils.showShort("密码修改成功，请重新登录");
                            Intent intent = new Intent(UpdatePasswordActivity.this, LoginActivity.class);
                            intent.putExtra("userName", UserInfoSPManager.getUserName());
                            intent.setFlags(UPDATE_PASSWORD_SUCCESS);
                            UserInfoSPManager.removeUserInfo();
                            startActivity(intent);
                            UpdatePasswordActivity.this.finish();
                        }
                    });
        }
    }

    private boolean checkInput(String password, String newPassword, String sureNewPassword) {
        if (TextUtils.isEmpty(password)) {
            ToastUtils.showShort("当前不能为空");
        } else if (TextUtils.isEmpty(newPassword)) {
            ToastUtils.showShort("新密码不能为空");
        } else if (TextUtils.isEmpty(sureNewPassword)) {
            ToastUtils.showShort("确认密码不能为空");
        } else {
            return true;
        }
        return false;
    }

    @OnClick({R.id.back_iv, R.id.save_tv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back_iv:
                this.finish();
                break;
            case R.id.save_tv:
                updatePassword();
                break;
        }
    }

}

